﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
    using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.Base.Location;
    using UFIDA.U9.CBO.FI.BankAccount;
    using UFIDA.U9.CBO.FI.UBankCode;
    using UFIDA.U9.CBO.FI.UOpenAccountBE;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

    /// <summary>
    /// BankSave partial 
    /// </summary>	
    public partial class BankSave
    {	
		internal BaseStrategy Select()
		{
			return new BankSaveImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class BankSaveImpementStrategy : BaseStrategy
	{
		public BankSaveImpementStrategy() { }

		public override object Do(object obj)
		{						
			BankSave bpObj = (BankSave)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<CopyOfCommonArchiveDataDTOData> retlist = new List<CopyOfCommonArchiveDataDTOData>();
                foreach (var dto in bpObj.Data)
                {
                    Bank be = Bank.Finder.Find($"Code='{dto.Code}'");

                    if (be == null)
                    {
                        //创建银行档案
                        be = CreateBank(dto);
                    }
                    else
                    {
                        //修改银行档案
                        be = UpdateBank(be, dto);
                    }

                    //处理返回值
                    retlist.Add(new CopyOfCommonArchiveDataDTOData
                    {
                        ID = be.ID,
                        Code = be.Code,
                        Name = be.Name,
                    });
                }

                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }

        private Bank UpdateBank(Bank bank, BankAITDTOData dto)
        {
            using(ISession session = Session.Open())
            {
                SetValue(dto, bank);
                session.Commit();
            }

            return bank;
        }

        private Bank CreateBank(BankAITDTOData dto)
        {
            Bank bank = Bank.Create();
            using (ISession session = Session.Open())
            {
                SetValue(dto, bank);
                session.Commit();
            }
            return bank;
        }

        private void SetValue(BankAITDTOData dto, Bank bank)
        {
            bank.Code=dto.Code;
            bank.Name=dto.Name;
            //开户行
            bank.UOpenAccount = EntityUtils.GetEntityByCode<UOpenAccount>(dto.UOpenAccount);
            if (bank.UOpenAccount != null)
            {
                //地区
                bank.UBankArea=bank.UOpenAccount.Area;
            }
            //联行号
            bank.UnionBankCode = dto.UnionBankCode;
            //银行代码
            bank.UBankCode = EntityUtils.GetEntityByCode<UBankCode>(dto.UBankCode);
            //国家地区
            if (dto.Country != null)
            {
                bank.Country=EntityUtils.GetEntityByCode<Country>(dto.Country);
            }
            else
            {
                bank.Country = EntityUtils.GetEntityByCode<Country>("142");
            }
            //对账单方向
            bank.BankStatement = BankStatementEnum.Oppsite;
            //银行类别
            if (dto.BankCategory == null) throw new Exception($"BankCategory 为空");
            bank.BankCategory = EntityUtils.GetEntityByCode<BankCategory>(dto.BankCategory.Code);
            //有效期
            bank.Effective = new UFIDA.U9.Base.PropertyTypes.Effective()
            {
                IsEffective = true,
                EffectiveDate = DateTime.Now,
                DisableDate = DateTime.MaxValue,
            };
        }
    }

	#endregion
	
	
}